// miniprogram/pages/rankinglist/list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    weekUsers: [],
    users: [],
    userInfos: [],
    active: 1,
  },

  onTabChange(event) {
    this.setData({
      page: 1,
      active:event.detail.name
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const db = wx.cloud.database()
    db.collection('user').where({
      isMember: true,
      isOut: false
    }).orderBy('point', 'desc').get({
      success: res => {
        console.log(res.data)
        this.setData({ users: res.data })
      }
    })
    // 总积分榜
    db.collection('userInfo').where({
      // isOut: false
    }).orderBy('point', 'desc').get({
      success: res => {
        console.log(res.data)
        this.setData({ userInfos: res.data })
      }
    })
    //周积分榜
    db.collection('user').where({
      isMember: true,
      isOut: false
    }).orderBy('weekPoint', 'desc').get({
      success: res => {
        console.log(res.data)
        this.setData({ weekUsers: res.data })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    });
    console.log('active',this.data.active)
    wx.showNavigationBarLoading() //在标题栏中显示加载
    const db = wx.cloud.database()
    switch (this.data.active) {
      case 0:
        db.collection('user').where({
          isMember: true,
          isOut: false
        }).orderBy('weekPoint', 'desc').get({
          success: res => {
            console.log(res.data)
            this.setData({ weekUsers: res.data })
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
          }
        })
        break
      case 1:
        db.collection('user').where({
          isMember: true,
          isOut: false
        }).orderBy('point', 'desc').get({
          success: res => {
            console.log(res.data)
            if (res.data.length == 0) {
              this.setData({ isEmpty: true })
            } else {
              this.setData({ isEmpty: false })
            }
            this.setData({ users: res.data })
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
          }
        })
        break
      case 2:
        db.collection('userInfo').where({
          // isOut: false
        }).orderBy('point', 'desc').get({
          success: res => {
            console.log(res.data)
            this.setData({ userInfos: res.data })
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
          }
        })
        break
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    console.log('page',this.data.page)
    this.setData({
      page: this.data.page
    });
    const db = wx.cloud.database()
    let skip = (this.data.page - 1) * 20
    switch (this.data.active) {
      case 0:
        db.collection('user').where({
          isMember: true,
          isOut: false
        }).skip(skip).orderBy('weekPoint', 'desc').get({
          success: res => {
            if (res.data.length == 0) {
              this.data.page--
              this.setData({
                page: this.data.page
              });
            } else {
              this.setData({ weekUsers: this.data.weekUsers.concat(res.data) })
            }
          }
        })
        break
      case 1:
        db.collection('user').where({
          isMember: true,
          isOut: false
        }).skip(skip).orderBy('point', 'desc').get({
          success: res => {
            if (res.data.length == 0) {
              this.data.page--
              this.setData({
                page: this.data.page
              });
            } else {
              this.setData({ users: this.data.users.concat(res.data) })
            }
          }
        })
        break
      case 2:
        db.collection('userInfo').where({
          // isOut: false
        }).skip(skip).orderBy('point', 'desc').get({
          success: res => {
            console.log(res.data)
            if (res.data.length == 0) {
              this.data.page--
              this.setData({
                page: this.data.page
              });
            } else {
              this.setData({ userInfos: this.data.userInfos.concat(res.data) })
            }
          }
        })
        break
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onClickItem: function (e) {
    var user = e.currentTarget.dataset.user
    wx.navigateTo({
      url: '../userDetail/detail?openid=' + user._openid + '&nickName=' + user.nickName + '&avatarUrl=' + user.avatarUrl,
    })
  }
})